package cn.yi.newsys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.yi.newsys.pojo.News;
import cn.yi.newsys.service.NewsService;
import cn.yi.newsys.service.impl.NewsServiceImpl;

public class JsonNewsServlet extends HttpServlet {


	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		NewsService ns = new NewsServiceImpl() ;
		List<News> list = ns.showNews() ;
		// 响应 JSON 格式的数据
		// 将Java对象序列化成JSON字符串，该Java对象需实现序列化接口
		out.print(JSON.toJSONString(list)) ;
		out.flush();
		out.close();
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response) ;
	}

}
